<?php
class GoodsTypeModel extends Model{
	public $table= 'sdb_good';
	//添加商品类型
	public function addGoodsType($Data){
		if(empty($Data)){
			$this->error='数据不能为空';
			return false;
		}
		$this->validate=array(
			array('cat_name','nonull','类型名称不能为空',2,1)
		);
		if($this->create($Data)){
			$db=M('sdb_good');
			if($db->where(array('cat_name'=>$Data['cat_name']))->find()){
				$this->error='类型名称已经存在';
				return false;
			}
			return $state = $this->add();
			if($state){
				if($this->updateCache()){
					return $state;
				}
			}
			return false;
		}else{
			return false;
		}	
	}
	//
	public function editGoodsType($Data){

		$this->validate=array(
			array('cat_name','nonull','类型名称不能为空',2,1)
		);
		if($this->create($Data)){
			$db=M('sdb_good');
			if($db->where(array('cat_name'=>$Data['cat_name']))->find()){
				$this->error='类型名称已经存在';
				return false;
			}
			return $state = $this->edit();

		}else{
			return false;
		}	
	}
	
	//更新缓存
	public function updateCache(){
		$GoodsTypeData=$this->all();
		$cacheData=array();
		foreach($GoodsTypeData as $GoodsType){
			$cacheData[$GoodsType['cat_good']]=$GoodsType;
		}
		return cache("goodstype",$cacheData);
	}
	
}